package com.briup.controller;

import com.briup.bean.PageBean;
import com.briup.pojo.Book;
import com.briup.pojo.extend.BookExtend;
import com.briup.result.Result;
import com.briup.service.BookService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    //条件查询所有书籍 - 不分页
    @GetMapping
    private Result getByCondition(String keyword, Integer categoryId) {
        System.out.println("in bookController, getByCondition() ...");

        List<BookExtend> list = bookService.getBooksByCondition(keyword, categoryId);
        return Result.success(list);
    }

    //条件查询所有书籍 - 分页 - 自己手动分页
    @GetMapping("/page")
    private Result getByPageCondition(String keyword, Integer categoryId,Integer pageNum,Integer pageSize) {
        System.out.println("in bookController, getByPageCondition() ...");
        PageBean pageBean = bookService.getBooksByConditionAndPage(keyword, categoryId, pageNum, pageSize);
        return Result.success(pageBean);
    }

    //条件查询所有书籍 - 分页 - 插件分页 PageHelper
    @ApiOperation(value="条件查询图书信息带分页",notes = "采用分页插件完成")
    @GetMapping("/pageNew")
    private Result getByPageConditionNew(String keyword, Integer categoryId,Integer pageNum,Integer pageSize) {
        System.out.println("in bookController, getByPageConditionNew() ...");
        PageInfo pageInfo = bookService.getBooksByConditionAndPageNew(keyword, categoryId, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    // 新增图书 - 前端传参 json对象 @RequestBody
    @PostMapping
    private Result add(@RequestBody Book book){
        bookService.addBook(book);
        return Result.success("添加图书成功");
    }

    // 批量删除图书 前端传参是一个数组 [1,2]
    @DeleteMapping
    private Result removeBatchByIds(@RequestBody List<Integer> ids){
        bookService.removeBatchByIds(ids);
        return Result.success();
    }
}
